<template>
  <div class="login-container">
    <el-form autoComplete="on" :model="registerForm" :rules="registerRules" ref="registerForm" label-position="right"
             label-width="70px" class="card-box login-form">
      <h3 class="title">注册</h3>

      <el-form-item prop="username" label="用户名">
        <el-input v-model="registerForm.username" autoComplete="on"/>
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input :type="pwdType" v-model="registerForm.password" autoComplete="on"/>
        <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye"/></span>
      </el-form-item>
      <el-form-item prop="confirm" label="确认密码">
        <el-input :type="pwdType" v-model="registerForm.confirm" autoComplete="on"/>
        <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye"/></span>
      </el-form-item>
      <el-form-item prop="imgCode" label="验证码">
        <el-input v-model="registerForm.imgCode" style="width: 50%;"/>
        <img @click="getCaptcha" :src="img" alt="" style="float: right;height: 47px">
      </el-form-item>
      <el-button type="primary" style="width:100%;margin-top: 5px" :loading="loading"
                 @click.native.prevent="handleRegister">
        注册
      </el-button>
    </el-form>
  </div>
</template>

<script>

  import {register, checkName} from "@/api/user";
  import {getCaptchaImg} from "@/api/common";

  export default {

    created() {
      this.getCaptcha()
    },

    data() {

      const validUsername = (rule, value, callback) => {
        if (!value) {
          callback(new Error('用户名不能为空'))
        } else {
          checkName(value).then(response => {
            if (response.data.duplicated === 1) {
              callback(new Error('该账号已被注册'))
            } else {
              callback()
            }
          })
        }
      }

      const validPass = (rule, value, callback) => {
        if (!value) {
          callback(new Error('密码不能为空'))
        } else if (value.length < 3) {
          callback(new Error('密码不能小于3位'))
        } else {
          callback()
        }
      }

      const validConfirm = (rule, value, callback) => {
        if (!value) {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.registerForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };

      return {
        registerForm: {
          username: '',
          password: '',
          imgCode: '',
          imgId: ''
        },
        registerRules: {
          username: [{trigger: 'blur', validator: validUsername}],
          password: [{trigger: 'blur', validator: validPass}],
          confirm: [{trigger: 'blur', validator: validConfirm}]
        },
        loading: false,
        confirm: '',
        pwdType: 'password',
        img: ''
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleRegister() {
        this.$refs.registerForm.validate(valid => {
          if (valid) {
            this.loading = true
            register(this.registerForm).then(() => {
              this.loading = false
              this.handleLogin()
            }).catch(() => {
              this.loading = false
            })
          } else {
            return false
          }
        })
      },
      getCaptcha() {
        getCaptchaImg().then(response => {
          this.registerForm.imgId = response.data.imgId
          this.img = 'data:image/png;base64,' + response.data.img
        });
      },
      handleLogin() {
        this.$store.dispatch('Login', this.registerForm).then(() => {
          this.loading = false
          this.$router.push({path: '/'})
        }).catch(() => {
          this.loading = false
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;

  .login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: $bg;
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0 1000px #293444 inset !important;
      -webkit-text-fill-color: #fff !important;
    }
    input {
      background: transparent;
      border: 0;
      -webkit-appearance: none;
      border-radius: 0;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
    }
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0 auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 400px;
      padding: 35px 35px 15px 35px;
      margin: 120px auto;
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .thirdparty-button {
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
  }
</style>
